VFG.MeasureArea = function(viewer) {
	this.viewer = viewer;
	this.positions = [];
	this.tempPoints = [];
	this.polygon = null;
	this.cartesian = null;
	this.areaLabel=null;
	this.floatingPoint = [];
	this.enity;
	this.tip = new VFG.Tip(this.viewer);
	this.radiansPerDegree = Math.PI / 180.0;//角度转化为弧度(rad) 
    this.degreesPerRadian = 180.0 / Math.PI;//弧度转化为角度
	this.init();
}
VFG.MeasureArea.prototype.init = function() {
	let _this = this;
	this.handler = new Cesium.ScreenSpaceEventHandler(_this.viewer.scene.canvas);
	this.handler.setInputAction(function(movement) {
		_this.tip.showAt(movement.endPosition, "单击开始，右击结束");
		_this.cartesian = VFG.Util.getScreenToC3(_this.viewer, movement.endPosition, _this.enity);
		if (_this.positions.length >= 2) {
			if (!_this.polygon) {
				_this.polygon = _this.viewer.entities.add({
					name: '直线',
					polygon: {
						hierarchy: [],
						//material: Cesium.Color.GREEN.withAlpha(0.5),
						material: Cesium.Color.fromCssColorString('#ffff00').withAlpha(0.5),
						classificationType: Cesium.ClassificationType.BOTH,
					}
				});
			} else {
				_this.positions.pop();
				_this.positions.push(_this.cartesian);
				_this.polygon.polygon.hierarchy = _this.update();
			}
			_this.tip.showAt(movement.endPosition, '单击拾取点，右击结束');
		}
	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

	this.handler.setInputAction(function(movement) {
		_this.cartesian = VFG.Util.getScreenToC3(_this.viewer, movement.position, _this.enity);
		if (_this.positions.length == 0) {
			_this.positions.push(_this.cartesian.clone());
		}
		_this.positions.push(_this.cartesian);

		var cartographic = Cesium.Cartographic.fromCartesian(_this.positions[_this.positions.length - 1]);
		var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
		var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
		var heightString = cartographic.height;
		_this.tempPoints.push({ lon: longitudeString, lat: latitudeString, hei: heightString });
		//在三维场景中添加Label
		_this.addPoint();
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

	this.handler.setInputAction(function(movement) {
		//_this.positions.pop(); //最后一个点无效
		_this.stop();
		_this.addPoint();
		_this.addLabel();
		if(_this.success){
			_this.success();
		}
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

};

VFG.MeasureArea.prototype.update = function() {
	var _this = this;
	return new Cesium.CallbackProperty(function() {
		return new Cesium.PolygonHierarchy(_this.positions);
	}, false);
}

VFG.MeasureArea.prototype.addLabel = function() {
	var _this = this;
	var textArea = _this.getArea(_this.tempPoints) + "平方公里";
	_this.areaLabel = _this.viewer.entities.add({
		name: '多边形面积',
		position: _this.positions[_this.positions.length - 1],
		label: {
			text: textArea,
			font: '18px sans-serif',
			fillColor: Cesium.Color.GOLD,
			style: Cesium.LabelStyle.FILL_AND_OUTLINE,
			outlineWidth: 2,
			verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
			pixelOffset: new Cesium.Cartesian2(20, -40),
			heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
		}
	});
}

VFG.MeasureArea.prototype.addPoint = function() {
	var _this = this;
	_this.floatingPoint.push(_this.viewer.entities.add({
		name: '多边形面积',
		position: _this.positions[_this.positions.length - 1],
		point: {
			pixelSize: 5,
			color: Cesium.Color.RED,
			outlineColor: Cesium.Color.WHITE,
			outlineWidth: 2,
			heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
		}
	}));
}

VFG.MeasureArea.prototype.getArea = function(points) {
	var _this = this;
	var res = 0;
	//拆分三角曲面
	for (var i = 0; i < points.length - 2; i++) {
		var j = (i + 1) % points.length;
		var k = (i + 2) % points.length;
		var totalAngle = _this.angle(points[i], points[j], points[k]);
		var dis_temp1 = _this.distance(_this.positions[i], _this.positions[j]);
		var dis_temp2 = _this.distance(_this.positions[j], _this.positions[k]);
		res += dis_temp1 * dis_temp2 * Math.abs(Math.sin(totalAngle));
	}
	return (res / 1000000.0).toFixed(4);
}

/*角度*/
VFG.MeasureArea.prototype.angle = function(p1, p2, p3) {
	var _this = this;
	var bearing21 = _this.bearing(p2, p1);
	var bearing23 = _this.bearing(p2, p3);
	var angle = bearing21 - bearing23;
	if (angle < 0) {
		angle += 360;
	}
	return angle;
}

/*方向*/
VFG.MeasureArea.prototype.bearing = function(from, to) {
	var _this = this;
	var lat1 = from.lat * _this.radiansPerDegree;
	var lon1 = from.lon * _this.radiansPerDegree;
	var lat2 = to.lat * _this.radiansPerDegree;
	var lon2 = to.lon * _this.radiansPerDegree;
	var angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));
	if (angle < 0) {
		angle += Math.PI * 2.0;
	}
	angle = angle * _this.degreesPerRadian;//角度
	return angle;
}

VFG.MeasureArea.prototype.distance = function(point1, point2) {
	var point1cartographic = Cesium.Cartographic.fromCartesian(point1);
	var point2cartographic = Cesium.Cartographic.fromCartesian(point2);
	var geodesic = new Cesium.EllipsoidGeodesic();
	geodesic.setEndPoints(point1cartographic, point2cartographic);
	var s = geodesic.surfaceDistance;
	s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
	return s;
}

VFG.MeasureArea.prototype.addListener=function(callback){
	if(callback!=null){
		this.success=callback;
	}
}

VFG.MeasureArea.prototype.removeListener=function(){
	this.success=null;
}

VFG.MeasureArea.prototype.stop = function() {
	if (this.tip) {
		this.tip.destroy();
	}
	if (this.handler) {
		this.handler.destroy();
		this.handler = null;
	};
}

VFG.MeasureArea.prototype.destroy = function() {
	var _this = this;
	if (this.tip) {
		this.tip.destroy();
	}
	if (this.handler) {
		this.handler.destroy();
		this.handler = null;
	};
	if(this.areaLabel){
		this.viewer.entities.remove(this.areaLabel);
	}
	if (this.polygon) {
		this.viewer.entities.remove(this.polygon);
	}
	if (this.floatingPoint && _this.floatingPoint.length > 0) {
		for (var i = 0; i < _this.floatingPoint.length; i++) {
			this.viewer.entities.remove(_this.floatingPoint[i]);
		}
	}
	delete this.polygon
	delete this.areaLabel
	delete this.floatingPoint
	delete this.handler
	delete this.viewer
	delete this.tempPoints
	delete this.cartesian
};